package com.chenfan.mcn.extension.convertor;

import com.chenfan.mcn.extension.convertor.annotation.FillBase;
import com.chenfan.mcn.extension.convertor.annotation.IgnoreBase;
import com.chenfan.mcn.extension.convertor.annotation.InitBase;
import com.chenfan.mcn.extension.mybatisplus.BaseAssembler;
import org.mapstruct.*;

/**
 * 忽略-BaseEntity类转换器 注：@AfterMapping @BeforeMapping 可以忽略/填充公共字段
 *
 * @author wulg
 * @date 2021-09-10
 **/
public interface BaseConvertor {

    /** 忽略公共参数字段
     * @Named("ignoreBase") => @BeanMapping(qualifiedByName = "ignoreBase")
     * @IgnoreBase => @BeanMapping(qualifiedBy = IgnoreBase.class)
     * @param source
     * @param target
     * @param <S>
     * @param <T>
     */
    @Named("ignoreBase")
    @IgnoreBase
    @AfterMapping
    default <S extends BaseAssembler, T extends BaseAssembler> void ignoreBase(S source, @MappingTarget T target) {
        target.ignore();
    }

    /**
     * 填充忽略公共参数字段
     * @param source
     * @param target
     * @param <S>
     * @param <T>
     */
    @Named("fillBase")
    @FillBase
    @AfterMapping
    default <S extends BaseAssembler, T extends BaseAssembler> void fillBase(S source, @MappingTarget T target) {
        target.fill();
    }

    /**
     * 初始化公共参数字段
     * @param source
     * @param target
     * @param <S>
     * @param <T>
     */
    @Named("initBase")
    @InitBase
    @AfterMapping
    default <S extends BaseAssembler, T extends BaseAssembler> void initBase(S source, @MappingTarget T target) {
        target.init();
    }
}
